package com.leyou.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.dto.SpecParamDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.entity.Spu;
import com.leyou.entity.SpuDetail;
import com.leyou.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SpecParamService specParamService;


    //todo 查询商品规格参数键值对
    @Override
    public List<SpecParamDTO> querySpecsValues(Long spuId, Boolean searching) {
//            // 1.准备规格参数value
//            // 1.1.根据id查询spuDetail
//            SpuDetail detail =  getById(id);
//            // 1.2.获取其中的规格参数键值对，转为一个map
//            Map<Long, Object> specValues = JsonUtils.toMap(detail.getSpecification(), Long.class, Object.class);
//
//            // 2.准备规格参数对象
//            // 2.1.根据id查询spu，获取商品分类信息
//            Spu spu = spuMapper.selectById(id);
//            // 2.2.根据分类id查询规格参数集合，如果searching不为空，还要加上searching条件
//            List<SpecParamDTO> params = specParamService.listSpecParam(spu.getCid3(), searching, null);
//
//            // 3.找到param的value，并存储
//            for (SpecParamDTO param : params) {
//                param.setValue(specValues.get(param.getId()));
//            }
//            return params;
        //todo 根据商品id查询对应的spu
        Spu spu = this.spuService.getById(spuId);
        if (spu==null){
            throw new LyException(204,"查询的商品不存在");
        }
        //1、查询规格参数
        List<SpecParamDTO> specParamDTOS = this.specParamService.listSpecParam(spu.getCid3(), searching, null);
        //判断查询条件下的规格参数组是否存在
        if (CollectionUtils.isEmpty(specParamDTOS)){
            //TODO,如果商品在，规格，品牌，分类，三个有一个缺失，说明数据库被恶意删除了，一定要报警
            throw new LyException(500,"商品对应得规格参数不存在");
        }
        //2\查找规格参数对应得属性值
        SpuDetail spuDetail = this.spuDetailService.getById(spuId);
        if (spuDetail==null){
            throw new LyException(500,"商品对应得详情不存在");
        }
        //json字符串，1，java，map，2，js，对象,map的key，就是规格参数的id，就是specParamDTO.getId(),value就是对应的规格参数的值
        Map<Long, Object> specParamMap =
                JsonUtils.nativeRead(spuDetail.getSpecification(), new TypeReference<Map<Long, Object>>() {
        });
        //适配器规格参数以及属性
        specParamDTOS.forEach(specParamDTO -> specParamDTO
                .setValue(specParamMap
                        .get(specParamDTO.getId()
                        )));


        return specParamDTOS;
    }

    //todo 根据id查询商品spu及sku、spuDetail等
    @Override
    public SpuDTO queryGoodsDetailById(Long id) {
        //1、查询spu
        PageDTO<SpuDTO> spuDTOPageDTO = this.spuService.querySpuByPage(id, null, null, 1, 1, null);

        //判断查询对象是否有效
        if (spuDTOPageDTO!=null&&CollectionUtils.isEmpty(spuDTOPageDTO.getItems())){
            SpuDTO spuDTO = spuDTOPageDTO.getItems().get(0);
            //根据spuid查询对应的spuDetail
            spuDTO.setSpuDetailDTO(this.spuDetailService.querySpuDetailById(id));
            //根据spuId查询对应得sku集合
            spuDTO.setSkus(this.skuService.listSkuBySpuId(id));
            return spuDTO;
        }else {
            throw new LyException(204,"对应得数据不存在");
        }
    }
}
